<template>
  <div class="login-page">
    <el-card>
      <template #header><p>黑马面经运营后台</p></template>
      <!-- 登录表单 -->
      <el-form :model="FormData" :rules="rules" ref="ruleForm">
        <!-- 第一级 -->
      <el-form-item label="用户名"  prop="username">
        <el-input placeholder="请输入用户名" v-model="FormData.username"></el-input>
      </el-form-item>
       <!-- 第二级 -->
       <el-form-item label="密码"  prop="password">
        <el-input placeholder="请输入密码" v-model="FormData.password" ></el-input>
      </el-form-item>
       <!-- 第三级 -->
       <el-form-item>
        <el-button type="primary" @click="doclick">登录</el-button>
        <el-button type="info" @click="doclick1">重置</el-button>
      </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
export default {
  name: 'login-page',
  data () {
    return {
      FormData: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          // 非空
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          // 正则
          { pattern: /^\w{5,15}$/, message: '用户名5-15位', trigger: 'blur' }
        ],
        password: [
          // 非空
          { required: true, message: '密码不能为空', trigger: 'blur' },
          // 正则
          { pattern: /^\w{5,15}$/, message: '密码5-15位', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    ...mapActions('user', ['getUser']),
    // 登录按钮
    doclick () {
      // 若 账号和密码都可以登录 则跳转登录页面 否则提示用户 请输入正确的账号和密码
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          // 发送ajax
          await this.getUser(this.FormData)
          // 跳转首页
          this.$router.push('/layout')
        } else {
          this.$message('请输入账号密码')
        }
      })
    },
    //  重置按钮
    doclick1 () {
      // 重置表单
      this.$refs.ruleForm.resetFields()
      // 清空表单内容
      this.FormData = {
        username: '',
        password: ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;
  .el-card {
    width: 420px;
    /*
    1.deep 深度选择器的作用: 让父组件可以选择 子组件内部的盒子
    2.deep语法:- ::v-deep (scss)    /deep/  (less)
    3.应用场景:需要在子组件中选择父组件的盒子
    */
    ::v-deep .el-card__header{
      text-align: center;
      font-size: 20px;
      color: #fff;
      background-color: #727cf5;
    }
  }
  .el-form {
    padding: 0 20px;
  }
  .tc {
    text-align: center;
  }
}

</style>
